package com.wnhz.work.yz.fragment.yushou;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.AllYuShouActivity;
import com.wnhz.work.yz.adapter.YuShouAdapter;
import com.wnhz.work.yz.bean.YuShouListBean;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by admin on 2017/4/12.
 */
public class AuditYuShouFragment extends Fragment {

    private View view;
    private AllYuShouActivity activity;
    private RecyclerView recycler;

    public void setActivity(AllYuShouActivity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.item_yushou_recycler, container, false);
      //  activity = (AllYuShouActivity) getActivity();
        initView();
        return view;
    }

    private void initView() {
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        upLoad();
    }

    private List<YuShouListBean.InfoEntity> infos = new ArrayList<>();
    boolean flag = false;

    @Override
    public void onHiddenChanged(boolean hidden) {
        upLoad();
        super.onHiddenChanged(hidden);
    }


    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", 2);
      //  activity = (AllYuShouActivity) getActivity();
        activity.showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }
        XUtil.Post(Url.BOOKING_BOOKINGORDERLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtil.e("----", "onSuccess:= " + result);
                infos.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YuShouListBean listBean = gson.fromJson(result, YuShouListBean.class);
                        infos = listBean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                            activity.MyToast(info);
                        }
                        if (flag) {

                        }
                        flag = true;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                activity.closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                activity.closeDialog();
            }
        });
    }


    private void setData() {
        recycler.setAdapter(new YuShouAdapter(activity, infos));
    }

}
